﻿using AutoMapper;
using B.S.BaseData.Api.Read.Command;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain;
using B.S.BataBase.Domain.DTO;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace B.S.BaseData.Api.Read.CommandHandler
{
    public class UserShowCommandHandler : IRequestHandler<UserShowCommand, APIResult<ApiPaging<UserDTO>>>
    {
        private readonly IBaseRepository<UserModel> _userrepository;
        private readonly IBaseRepository<UserRoleModel> _userroleRepository;
        private readonly IBaseRepository<RoleModel> _roleroleRepository;
        private readonly IMapper _mapper;
        public UserShowCommandHandler(IBaseRepository<UserModel> userrepository,
            IBaseRepository<UserRoleModel> userroleRepository,
            IMapper mapper,
            IBaseRepository<RoleModel> roleroleRepository)
        {
            _userrepository = userrepository;
            _userroleRepository = userroleRepository;
            _mapper = mapper;
            _roleroleRepository = roleroleRepository;
        }
        public async Task<APIResult<ApiPaging<UserDTO>>> Handle(UserShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<ApiPaging<UserDTO>> result=new APIResult<ApiPaging<UserDTO>>();
            var list = _userrepository.GetAll().Where(x=>x.IsDeleted==false);
            if (request.userName != null)
            {
                list = list.Where(x=>x.UserName.Contains(request.userName));
            }
            var res = _mapper.Map<List<UserDTO>>(list);

            foreach (var item in res)
            {
                var data = _userroleRepository.GetAll().Where(x => x.UserId == item.Id).Select(x => x.RoleId).ToList();
                item.RoleList=data;
                item.RoleName = await GetRoleName(data);                                        
            }

            var totacount=res.Count();
            var pagecount = (int)Math.Ceiling(totacount*1.0/request.pageSize);

            ApiPaging<UserDTO> paging = new ApiPaging<UserDTO>();
            paging.TotalPage = pagecount;
            paging.PageData=res.OrderByDescending(x=>x.UserCreateTime).Skip((request.pageIndex-1)&request.pageSize).Take(request.pageSize).ToList();
            paging.TotalCount = totacount;
            result.Code=ApiEnum.Success;
            result.Msg = "用户列表显示成功";
            result.Data= paging;

            return await Task.FromResult(result);
        }
        /// <summary>
        /// 根据角色id集合获取id名称
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<string> GetRoleName(List<long> ids)
        {
            string s = "";
            foreach (var id in ids)
            {
                s += (await _roleroleRepository.GetValues(x=>x.Id==id)).RoleName+",";
            }
            return s.TrimEnd(',');
        }
        

    }
}
